package com.cheetah.start.common.shoesImg;

import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.List;

public class ResultVisualizer {

    /**
     * 可视化相似度结果
     */
    public static Mat visualizeSimilarityResults(Mat originalImage,
                                                 List<MatOfPoint> contours,
                                                 List<SimilarityMatrix> matrices) {
        Mat resultImage = originalImage.clone();

        // 为每个轮廓分配颜色
        List<Scalar> colors = generateColors(contours.size());

        // 绘制所有轮廓
        for (int i = 0; i < contours.size(); i++) {
            List<MatOfPoint> contourList = new ArrayList<>();
            contourList.add(contours.get(i));

            Imgproc.drawContours(resultImage, contourList, -1, colors.get(i), 3);

            // 添加轮廓编号
            Rect rect = Imgproc.boundingRect(contours.get(i));
            Imgproc.putText(resultImage, String.valueOf(i + 1),
                    new Point(rect.x + rect.width / 2 - 10, rect.y + rect.height / 2),
                    Imgproc.FONT_HERSHEY_SIMPLEX, 1, colors.get(i), 2);
        }

        return resultImage;
    }

    /**
     * 生成相似度报告
     */
    public static void generateSimilarityReport(List<SimilarityMatrix> matrices,
                                                List<MatOfPoint> contours) {
        System.out.println("\n=== 轮廓相似度分析报告 ===");
        System.out.println("共分析 " + contours.size() + " 个轮廓");
        System.out.println("\n相似度排名（从高到低）:");
        System.out.println("==============================================");

        for (int i = 0; i < Math.min(matrices.size(), 10); i++) { // 显示前10个
            SimilarityMatrix matrix = matrices.get(i);
            SimilarityResult sim = matrix.similarity;

            System.out.printf("排名 %d: 轮廓%d ↔ 轮廓%d\n", i + 1, matrix.index1 + 1, matrix.index2 + 1);
            System.out.printf("  综合相似度: %.1f%%\n", sim.overallSimilarity * 100);
            System.out.printf("  颜色相似度: %.1f%%\n", sim.colorSimilarity * 100);
            System.out.printf("  纹理相似度: %.1f%%\n", sim.textureSimilarity * 100);
            System.out.printf("  形状相似度: %.1f%%\n", sim.shapeSimilarity * 100);
            System.out.printf("  边缘相似度: %.1f%%\n", sim.edgeSimilarity * 100);

            // 显示轮廓面积信息
            double area1 = Imgproc.contourArea(contours.get(matrix.index1));
            double area2 = Imgproc.contourArea(contours.get(matrix.index2));
            System.out.printf("  轮廓面积: %.0f ↔ %.0f\n", area1, area2);
            System.out.println("----------------------------------------------");
        }

        // 统计信息
        if (!matrices.isEmpty()) {
            double maxSimilarity = matrices.get(0).similarity.overallSimilarity * 100;
            double minSimilarity = matrices.get(matrices.size() - 1).similarity.overallSimilarity * 100;
            double avgSimilarity = matrices.stream()
                    .mapToDouble(m -> m.similarity.overallSimilarity * 100)
                    .average()
                    .orElse(0);

            System.out.printf("\n统计信息:\n");
            System.out.printf("最高相似度: %.1f%%\n", maxSimilarity);
            System.out.printf("最低相似度: %.1f%%\n", minSimilarity);
            System.out.printf("平均相似度: %.1f%%\n", avgSimilarity);
        }
    }

    /**
     * 生成颜色列表
     */
    private static List<Scalar> generateColors(int count) {
        List<Scalar> colors = new ArrayList<>();
        float hueStep = 360.0f / count;

        for (int i = 0; i < count; i++) {
            float hue = i * hueStep;
            // HSV to BGR
            Mat hsv = new Mat(1, 1, CvType.CV_8UC3, new Scalar(hue, 255, 255));
            Mat bgr = new Mat();
            Imgproc.cvtColor(hsv, bgr, Imgproc.COLOR_HSV2BGR);
            double[] color = bgr.get(0, 0);
            colors.add(new Scalar(color[0], color[1], color[2]));
        }

        return colors;
    }




}
